Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
從陣列中,求出最大連續元素的總和。
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Input: nums = [1]
Output: 1
Input: nums = [0]
Output: 0
Input: nums = [-1]
Output: -1
遍歷陣列,若連續元素相加的值大於最大值max,則取代max。
var maxSubArray = function(nums) {
    let sum = 0;
    let max = nums[0];
    for (let i = 0; i < nums.length; i++) {
        for (let j = i; j < nums.length; j++) {
            sum += nums[j];
            if (max < sum) max = sum;
        }
        sum = 0;
    }
    return max;
};